package com.appspot.moviesessions;

import java.io.InputStream;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;

import com.appspot.moviesessions.provider.MovieSession;

public class AndroidFactory implements CursorAdapterFactory, UriParser,
		com.appspot.moviesessions.BitmapFactory, CursorLoaderFactory,
		IntentFactory, ContentUri {

	private static AndroidFactory INSTANCE = null;

	private AndroidFactory() {

	}

	public static AndroidFactory getInstance() {
		if (INSTANCE == null) {
			synchronized (AndroidFactory.class) {
				if (INSTANCE == null) {
					INSTANCE = new AndroidFactory();
				}
			}
		}
		return INSTANCE;
	}

	@Override
	public SimpleCursorAdapter create(Context context, int layout,
			Cursor cursor, String[] from, int[] to, int flags) {
		return new SimpleCursorAdapter(context, layout, cursor, from, to, flags);
	}

	@Override
	public Drawable bitmapDrawable(InputStream stream) {
		return new BitmapDrawable(
				android.graphics.BitmapFactory.decodeStream(stream));
	}

	@Override
	public Uri parse(String uri) {
		return Uri.parse(uri);
	}

	@Override
	public Loader<Cursor> create(Context context, Uri resource,
			String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		return new CursorLoader(context, resource, projection, selection,
				selectionArgs, sortOrder);
	}

	@Override
	public Intent launchMovieInfoIntent(Context context, long id) {
		return new Intent("", Uri.parse(String.format(
				MovieSession.FORMAT_MOVIE_INFO_URI, id)), context,
				MovieInfoActivity.class);
	}

	@Override
	public Intent launchMovieSessionIntent(Context context, long id) {
		return new Intent("", Uri.parse(String.format(
				MovieSession.FORMAT_MOVIE_SESSION_URI, id)), context,
				MovieSessionActivity.class);
	}

	@Override
	public long parseId(String uri) {
		return ContentUris.parseId(parse(uri));
	}

}
